| Conditions | 28 |
| Total Lines | 101 |
| Code Lines | 67 |
| Lines | 101 |
| Ratio | 100 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like directive.message.js ➔ MessageConstructor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | |||
| 8 | function MessageConstructor($scope, $element, $attrs) { |
||
| 9 | var constructor = this; |
||
|
|
|||
| 10 | var $ctrl = $scope.msgCtrl; |
||
| 11 | var tagName = $element[0].tagName.toLowerCase(); |
||
| 12 | |||
| 13 | $scope.autoClose = false; |
||
| 14 | var DirectiveProperties = (function () { |
||
| 15 | var autoClose; |
||
| 16 | var type; |
||
| 17 | |||
| 18 | function findAutoClose(){ |
||
| 19 | var object = $attrs.autoClose; |
||
| 20 | if(typeof(object) != "undefined") |
||
| 21 | return true; |
||
| 22 | else |
||
| 23 | return false; |
||
| 24 | } |
||
| 25 | function findType(){ |
||
| 26 | var object = $attrs.type; |
||
| 27 | if(typeof(object) != "undefined") |
||
| 28 | return object; |
||
| 29 | else |
||
| 30 | return "alert"; |
||
| 31 | } |
||
| 32 | |||
| 33 | return { |
||
| 34 | getAutoClose: function () { |
||
| 35 | if (!autoClose) { |
||
| 36 | autoClose = findAutoClose(); |
||
| 37 | } |
||
| 38 | $scope.autoClose = autoClose; |
||
| 39 | return autoClose; |
||
| 40 | }, |
||
| 41 | getType: function () { |
||
| 42 | if (!type) { |
||
| 43 | type = findType(); |
||
| 44 | } |
||
| 45 | $scope.type = type; |
||
| 46 | return type; |
||
| 47 | } |
||
| 48 | }; |
||
| 49 | })(); |
||
| 50 | |||
| 51 | function TryToCallInitDirective(){ |
||
| 52 | if(typeof $scope.InitDirective == "function"){ |
||
| 53 | $scope.InitDirective($scope, $element, $attrs, $ctrl); |
||
| 54 | }else{ |
||
| 55 | $scope.DefaultInitDirective(); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | $scope.DefaultInitDirective = function(){ |
||
| 59 | if(Core.GetConfig().debugLog.DirectiveFlow) |
||
| 60 | console.log("scope.$id:"+$scope.$id+", may implement $scope.InitDirective() function in webapge"); |
||
| 61 | } |
||
| 62 | function InitializeMessage() { |
||
| 63 | DirectiveProperties.getAutoClose(); |
||
| 64 | DirectiveProperties.getType(); |
||
| 65 | |||
| 66 | $ctrl.ngModel = []; |
||
| 67 | |||
| 68 | $ctrl.ngModel = MessageService.getMsg(); |
||
| 69 | $scope.ngModel = $ctrl.ngModel; |
||
| 70 | } |
||
| 71 | $scope.Test = function(){ |
||
| 72 | console.dir($ctrl) |
||
| 73 | console.dir($ctrl.ngModel) |
||
| 74 | } |
||
| 75 | $scope.Initialize = function(){ |
||
| 76 | $scope.InitScope(); |
||
| 77 | TryToCallInitDirective(); |
||
| 78 | } |
||
| 79 | $scope.InitScope = function(){ |
||
| 80 | InitializeMessage(); |
||
| 81 | } |
||
| 82 | |||
| 83 | function InitDirective(){ |
||
| 84 | if(Core.GetConfig().debugLog.DirectiveFlow) |
||
| 85 | console.log("scope.$id:"+$scope.$id+", may implement $scope.InitDirective() function in webapge"); |
||
| 86 | } |
||
| 87 | // $scope.Initialize(); |
||
| 88 | |||
| 89 | $scope.$watchCollection( |
||
| 90 | function() { return $ctrl.ngModel }, |
||
| 91 | function(newValue, oldValue) { |
||
| 92 | if(typeof(newValue) == "undefined" && typeof(oldValue) == "undefined") |
||
| 93 | return; |
||
| 94 | |||
| 95 | var newValueLength = newValue.length; |
||
| 96 | var oldValueLength = oldValue.length; |
||
| 97 | |||
| 98 | if ( newValueLength !== oldValueLength ) { |
||
| 99 | // if(newValueLength > oldValueLength){ |
||
| 100 | if($scope == "alert") |
||
| 101 | if($scope.autoClose) |
||
| 102 | $timeout(function(){ |
||
| 103 | MessageService.shiftMsg(); |
||
| 104 | }, 7000); // (milliseconds), 1s = 1000ms |
||
| 105 | } |
||
| 106 | } |
||
| 107 | ); |
||
| 108 | } |
||
| 109 | function templateFunction(tElement, tAttrs) { |
||
| 159 |